package com.yunhe.rentcars.pro.controller.rent;


import com.yunhe.rentcars.commons.dto.JSONResponse;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.commons.util.ShareCodeUtils;
import com.yunhe.rentcars.pro.dao.client.ClientUserDao;
import com.yunhe.rentcars.pro.entity.client.ClientCompany;
import com.yunhe.rentcars.pro.entity.client.ClientUser;
import com.yunhe.rentcars.pro.entity.rent.CarOrder;
import com.yunhe.rentcars.pro.service.rent.CarOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import com.yunhe.rentcars.pro.entity.rent.Cars;
import com.yunhe.rentcars.pro.service.car.CarsService;
import com.yunhe.rentcars.pro.service.client.ClientUserService;
import com.yunhe.rentcars.pro.service.rent.CarOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/carOrder")
public class CarOrderController {

    @Autowired
    private CarOrderService carOrderService;
    @Autowired
    private CarsService carsService;
    @Autowired
    private ClientUserService clientUserService;

    @ResponseBody
    @RequestMapping("/save")
    public JSONResponse save(CarOrder carOrder, HttpServletRequest req){
        ClientUser clientUser = (ClientUser)req.getSession().getAttribute("clientUser");
        carOrder.setClientid(clientUser.getUid());
        carOrderService.saveCarOrder(carOrder);
        req.getSession().setAttribute("carOrder",carOrder);
        return JSONResponse.success("订单提交成功");
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONResponse delete(Long id){
        carOrderService.deleteCarOrder(id);
        return JSONResponse.success("");
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResponse update(CarOrder carOrder){
        carOrderService.updateCarOrder(carOrder);
        return JSONResponse.success("");
    }

    @RequestMapping("/updateOrderstate")
    @ResponseBody
    public JSONResponse updateOrderstate(CarOrder carOrder){
        carOrderService.updateOrderstate(carOrder);
        return JSONResponse.success("");
    }




    /*
       * 客户登记页面1
       */
    @RequestMapping("/customerRegis")
    public String addClient() {
        return "rent/CustomerRegis";
    }
    /*
     * 取还车时间地点页
     */
    @RequestMapping("/selectionAddr")
    public String save(ClientUser clientUser, ClientCompany clienCompany, HttpServletRequest request){

       /* clientUserService.saveCompany(clienCompany);
        clientUser.setCompanyId(clienCompany.getId());
        clientUserService.save(clientUser,clienCompany);

        HttpSession userSession = request.getSession();
        userSession.setAttribute("clientUser" , clientUser);
        HttpSession companySession = request.getSession();
        companySession.setAttribute("clienCompany",clienCompany);*/

        return "rent/SelectionAddr";
    }

    @RequestMapping("/saveTimeAndAddres")
    public String saveTimeAndAddres(CarOrder carOrder, HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("carOrder",carOrder);
        return "redirect:/carOrder/carList";
    }

    /*
    * 租车信息列表
    */
    @RequestMapping("/carList")
    public String  rentCarList() {
        return "rent/CarList";
    }

    /*
    * 订单提交页
     */
    @RequestMapping("/orderSub")
    public String orderSub(Long carid, String carcode, HttpServletRequest req){
        Cars cars = carsService.findOne(carid, carcode);
        CarOrder carOrder = (CarOrder) req.getSession().getAttribute("carOrder");
        String s = ShareCodeUtils.toSerialCode(carid);
        carOrder.setOrderid(s);
        HttpSession session = req.getSession();
        session.setAttribute("cars",cars);
        session.setAttribute("carOrder",carOrder);
        return "rent/OrderSub";
    }


    @RequestMapping("/rentHistory")
    public String rentHistory(){
        return "car/renthistory/list";
    }


    @RequestMapping("/rentHistoryData")
    @ResponseBody
    public Pageable<CarOrder> rentHistoryData(CarOrder carOrder, Integer offset, Integer limit){
        return carOrderService.findList(carOrder, offset, limit);
    }


    /*
    * 支付页
    */
    @RequestMapping("/payment")
    public String payment(Long id,String orderid,HttpServletRequest req){
        CarOrder carOrder = carOrderService.findCarOrder(id,orderid);
        ClientUser clientUser = null;
        if(null != carOrder){
            clientUser = clientUserService.find(carOrder.getClientid());
        }
        req.setAttribute("carOrder",carOrder);
        req.setAttribute("clientUser",clientUser);
        return "rent/payment";
    }

}
